var username=localStorage.getItem("username");
var token=localStorage.getItem("token");
var userid=localStorage.getItem("userid");
$(function(){
	var paramMap = params();
	var id = paramMap['id'];
	
	$("#backList").click(function(){
		var url=server_url+'/buyerManage/list.do';
		tl_config.last_url=url;
		tl_config.loadToWorkArea(url);
	});
	$("#sub").click(function(){
		if(!$("form").valid()){
			return;
		}
		$.ajax({
			type:'post',
			url:server_url+'/rest/buyerManage/updateBuyer.do?token='+token+'&ran='+Math.random(),
			data:$("form").serialize(),
			success:function(data){
				easyDialog.open({
					  container : {
						  header : '温馨提示',
					    content : '保存成功',
					    yesFn:function(){
							  var url=server_url+'/buyerManage/list.do';
								tl_config.last_url=url;
								tl_config.loadToWorkArea(url);
						  }
					  },
					  
					});
			}
		});
	});
	$("input[type=file]").change(function(){
		ajaxfileuploadUtil(this,"buyer");
	});
	
	$.ajax({
		type : 'GET',
		contentType : 'application/json',
		async : true,
		data : {
			buyerid : id,
			token:token
		},
		url : server_url+'/rest/buyerManage/getBuyer.do?ran='+Math.random(),
		dataType : 'json',
		success : function(data) {
			if (data.retCode == "0000") {
				pasteData(data.data);
			} else{
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});
			}
		}
	});
	$("#province").change(function(){
		areaParentChange("city", $(this).val(),"county");
	});
	$("#city").change(function(){
		areaParentChange("county", $(this).val());
	});
	
	//表单验证
	var validate = $("form").validate({		   
			rules:{					
				resgisterNo:{
					  required:true
				},
				taxNo:{
					   required:true,
					   stringMaxLength:30
				 },
				 bank:{
					   required:true,
					   stringMaxLength:30
				},
				bankCard:{
					   required:true,
					   stringMaxLength:30
				},
				purName:{
					   required:true,
					   stringMaxLength:30
				},
				purTel:{
					   required:true,
					   stringMaxLength:30
				},
				identCardNum:{
					   required:true,
					   stringMaxLength:30
				}
			},
			messages:{		
				resgisterNo:{
					required:"该值不能为空！"
				},
				taxNo:{
					required:"该值不能为空！",
	                stringMaxLength:"最多能输入30个字符！"
				 },
				 bank:{
					required:"该值不能为空！",
	                stringMaxLength:"最多能输入30个字符！"
				},
				bankCard:{
					required:"该值不能为空！",
	                stringMaxLength:"最多能输入30个字符！"
				},
				purName:{
					required:"该值不能为空！",
	                stringMaxLength:"最多能输入30个字符！"
				},
				purTel:{
					required:"该值不能为空！",
	                stringMaxLength:"最多能输入30个字符！"
				}
				
			},
			submitHandler:function(){
				sub();
			},
			errorPlacement: function(error, element) {
				if ( element.is(":radio") )
					error.appendTo ( element.parent() );
				else if ( element.is(":checkbox") )
					error.appendTo ( element.parent() );
				else if ( element.is("input[name=captcha]") )
					error.appendTo ( element.parent() );
				else
					error.insertAfter(element);
			},
			validClass : 'rightValidate',
		    success: function(label) {
			   label.html("&nbsp;").text(" ") ;
		    }
	 });	
	
	
});


function isEmpty(data){
	if(data==null||data==undefined||data.length==0){
		return true;
	}else{
		return false;
	}
}
function pasteData(data){
	console.log(data);
	var buyer = data.buyer;
	$("#buyerId").val(buyer.buyerId);
	$("#buyerName").text(buyer.buyerName);
	$("[name=buyerName]").val(buyer.buyerName);
	$("#buyerTel").text(buyer.buyerTel);
	$("[name=buyerTel]").val(buyer.buyerTel);
	$("#addTime").text(buyer.addTime);
	$("#address").val(buyer.address);

	var provinces = data.provinces;
	var citys = data.citys;
	var countrys = data.countrys;
	
	mapIfExist(function(province){
			$("<option value="+province.areaId+">"+province.areaName+"</option>").appendTo("#province");
		},provinces);
	mapIfExist(function(city){
			$("<option value="+city.areaId+">"+city.areaName+"</option>").appendTo("#city");
		},citys);
	mapIfExist(function(country){
			$("<option value="+country.areaId+">"+country.areaName+"</option>").appendTo("#county");
		},countrys);
	$("#province").val(buyer.province);
	$("#city").val(buyer.city);
	$("#country").val(buyer.county);
	var consignees = buyer.consignees;
	if(exist(consignees)){
		_.map(consignees,function(obj){
			$("<tr><td>"+obj.consigneer+"</td> " +
					"<td>"+obj.mobile+"</td>  " +
					"<td>"+obj.provinceName+obj.cityName+obj.countyName+obj.consigneerAddress+"</td>  " +
					"<td>&nbsp;</td>  " +
					"</tr>").appendTo($("table"));
		});
	}
	$("#remark").val(buyer.remark);
}

